package com.fdb.efp.nls.api.service;

import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.efp.nls.service.facade.AccLoanService;
import com.fdb.efp.nls.service.vo.AccLoanVO;
import com.fdb.efp.nls.service.facade.NlsApplyInfoService;
import com.fdb.efp.nls.service.vo.NlsApplyInfoVO;
import com.fdb.efp.nls.common.RscConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * 需求编号【2019D0509】
 *
 * @author qiuyf
 * @version 0.1 初始为0.1,后续变更则修改
 * @since 2019/09/0917:56
 */
@Service("partnerInfoFrontService")
public class PartnerInfoFrontService {

    private static Logger logger = LoggerFactory.getLogger(PartnerInfoFrontService.class);

    @Autowired
    @Qualifier("accLoanService")
    private AccLoanService accLoanService;

    @Autowired
    @Qualifier("nlsApplyInfoService")
    private NlsApplyInfoService nlsApplyInfoService;

    /**
     * 统计不良贷款余额
     * @param prdCodes 产品编号list
     * @return
     */
    public BigDecimal getBadLoanBalance(List<String> prdCodes){
        BigDecimal result = BigDecimal.ZERO;
        //根据产品编号获取借据信息
        for(String prdCode : prdCodes){
            AccLoanVO accLoanVO = new AccLoanVO();
            accLoanVO.setPrdCode(prdCode);
            List<AccLoanVO> accLoanVOList = accLoanService.queryAccLoanListByCondition(accLoanVO);
            if(Objects.nonNull(accLoanVOList) && !accLoanVOList.isEmpty()){
                BigDecimal loanBalances = accLoanVOList.stream()
                        .filter(accl -> { return RscConstant.STD_ZB_FIVE_SORT_30.equals(accl.getCla()) ||
                                RscConstant.STD_ZB_FIVE_SORT_40.equals(accl.getCla()) ||
                                RscConstant.STD_ZB_FIVE_SORT_50.equals(accl.getCla());})
                        .map(AccLoanVO::getLoanBalance)
                        .filter(Objects::nonNull)
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                result = result.add(loanBalances);
            }
            logger.debug("根据产品编号:"+ prdCode + "获取到的不良贷款余额总和为:"+result);
        }
        return result;
    }

    /**
     * 统计贷款总额
     * @param prdCodes 产品编号list
     * @return
     */
    public BigDecimal getAllLoanBalance(List<String> prdCodes){
        BigDecimal result = BigDecimal.ZERO;
        //根据产品编号获取借据信息
        for(String prdCode : prdCodes){
            AccLoanVO accLoanVO = new AccLoanVO();
            accLoanVO.setPrdCode(prdCode);
            List<AccLoanVO> accLoanVOList = accLoanService.queryAccLoanListByCondition(accLoanVO);
            if(Objects.nonNull(accLoanVOList) && !accLoanVOList.isEmpty()){
                BigDecimal loanBalances = accLoanVOList.stream()
                        .map(AccLoanVO::getLoanBalance)
                        .filter(Objects::nonNull)
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                result = result.add(loanBalances);
            }
            logger.debug("根据产品编号:"+ prdCode + "获取到的贷款余额总和为:"+result);
        }
        return result;
    }

    /**
     * 统计放款总金额
     * @param prdCodes 产品编号list
     * @return
     */
    public BigDecimal getAmountOfLoan(List<String> prdCodes){
        BigDecimal result = BigDecimal.ZERO;
        List<NlsApplyInfoVO> nlsApplyInfoVOList = nlsApplyInfoService.queryAllByPrdCodes(prdCodes);
        if(Objects.nonNull(nlsApplyInfoVOList) && !nlsApplyInfoVOList.isEmpty()){
            result = nlsApplyInfoVOList.stream()
                    .filter(nlsApplyInfoVO -> { return StringUtil.isStrNotEmpty(nlsApplyInfoVO.getAmountOfLoan());})
                    .map(nlsApplyInfoVO2 -> {
                        return new BigDecimal(nlsApplyInfoVO2.getAmountOfLoan());
                    })
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
        }
        return result;
    }


}
